package com.itheima.controller;

import com.itheima.dto.MessageConstant;
import com.itheima.dto.PageResult;
import com.itheima.dto.QueryPageBean;
import com.itheima.dto.Result;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.print.DocFlavor;
import java.util.List;

@RestController
@RequestMapping("/checkitem")
public class CheckItemController {

    @Autowired
    private CheckItemService checkItemService;

    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
    @PostMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = checkItemService.pageQuery(queryPageBean);
        return pageResult;
    }

    /**
     * 新增
     * @param checkItem
     * @return
     */
    @PostMapping
    public Result addCheckItem(@RequestBody CheckItem checkItem){
        checkItemService.addCheckItem(checkItem);
        return  Result.success(MessageConstant.ADD_CHECKITEM_SUCCESS);
    }

    /**
     * 根据id删除检查项
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result deleteCheckItem(@PathVariable Integer id){
        return checkItemService.deleteCheckItem(id)?
                Result.success(MessageConstant.DELETE_CHECKITEM_SUCCESS):
                Result.error("当前检查项已经被关联到检查组，不允许删除");
    }

    /**
     * 根据id查询用于修改回显
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result selectCheckItem(@PathVariable Integer id){
        CheckItem checkItem = checkItemService.selectCheckItem(id);
        return Result.success(checkItem,MessageConstant.QUERY_CHECKITEM_SUCCESS);
    }

    /**
     * 根据id修改检查项
     * @param checkItem
     * @return
     */
    @PutMapping
    public Result updateCheckItem(@RequestBody CheckItem checkItem){
        checkItemService.updateCheckItem(checkItem);
        return Result.success(MessageConstant.EDIT_CHECKITEM_SUCCESS);
    }

    /**
     * 查询全部检查项
     * @return
     */
    @GetMapping("/list")
    public Result selectCheckItemAll(){
        List<CheckItem> checkItems = checkItemService.selectCheckItemAll();
        return  Result.success(checkItems,MessageConstant.QUERY_CHECKITEM_SUCCESS);
    }
}
